//
//  NewCurveViewController.m
//  hospitalVIP
//
//  Created by han's mac on 12/10/13.
//  Copyright (c) 2013 MEETStudio. All rights reserved.
//

#import "NewCurveViewController.h"

@interface NewCurveViewController ()

@end

@implementation NewCurveViewController
{
    UIScrollView *scrollView;
    float scrollWidth;
}

@synthesize closeBtn;

@synthesize timer;

static ASIHTTPConnection *getData_connect;


- (id)initWithDataType:(int)type{
    
    self = [super init];
    if (self) {
        dataType = type;
    }
    return self;
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    
    //test
    //[self dataDone];
    
    [self loadData]
    ;
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollWidth = self.view.frame.size.width;
    [self.view addSubview:scrollView];
    
    UIColor * cc = [UIColor whiteColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:cc forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    switch (dataType) {
        case 1:
            [self.navigationItem setTitle:@"体重"];
            break;
        case 2:
            [self.navigationItem setTitle:@"尿蛋白"];
            break;
         case 3:
             [self.navigationItem setTitle:@"收缩压"];
            break;
        case 4:
            [self.navigationItem setTitle:@"舒张压"];
            break;
        case 5:
            [self.navigationItem setTitle:@"血糖"];
            break;
            
        default:
            break;
    }
   
    
    // Do any additional setup after loading the view from its nib.
    
}
-(void)closePage
{
    UINavigationController* naviController = (UINavigationController*)[[UIApplication sharedApplication] keyWindow].rootViewController;
    [naviController popViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIDeviceOrientationIsLandscape(interfaceOrientation);
}
#ifdef __IPHONE_6_0
-(BOOL)shouldAutorotate
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    return UIDeviceOrientationIsLandscape(orientation);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
#endif
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData{
    
    NSString *uid = [[NSUserDefaults standardUserDefaults] valueForKey:USER_ID];
    
    NSString *urlString = nil;
    
    switch (dataType) {
        case 1:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/WeightAction.aspx?UserID=%@",BASEURL,uid];
            break;
        case 2:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/UriaProteinAction.aspx?UserID=%@",BASEURL,uid];
            break;
        case 3:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/BloodPressureAction.aspx?UserID=%@",BASEURL,uid];
            break;
        case 4:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/BloodPressureAction.aspx?UserID=%@",BASEURL,uid];
            break;
        case 5:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/BloodSugarAction.aspx?UserID=%@",BASEURL,uid];
            break;
        case 6:
            urlString = [NSString stringWithFormat:@"%@/MobileHealth/HeartAction.aspx?UserID=%@",BASEURL,uid];
            break;
        default:
            break;
    }
    
    
    
    
    
    
	NSURL *url = [NSURL URLWithString:urlString];
    
    
    if (getData_connect != nil) {
        getData_connect.connectionDelegate = nil;
        
        getData_connect = nil;
    }
    
    getData_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    getData_connect.connectionDelegate = self;
    
    [getData_connect startAsynchronous];
    
    
    [LoadingView showDefaultLoadingView];
}


#pragma mark connection delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    [LoadingView hideWithAnimated:YES];
    
    CLog(@"data = %@",dict);
    if (dict) {
        NSArray *array = [dict objectForKey:@"datalist"];
        if (array != nil && array.count > 0) {
            
            if (dataArray == nil) {
                dataArray = [[NSMutableArray alloc]init];
            }
            else{
                [dataArray removeAllObjects];
            }
            [dataArray addObjectsFromArray:array];
            [self dataDone];
        }
    }
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    [LoadingView hideWithAnimated:YES];
    
}

- (void)dataDone{
    
    if(dataType==1)
    {
        
        
        
        NSMutableArray *points = [[NSMutableArray alloc] init];
        NSMutableArray *dates = [[NSMutableArray alloc] init];
        for (int i = 0; i <= dataArray.count -1; i++)
        {
            NSDictionary *dict = [dataArray objectAtIndex:i];
            
            NSString *DataUploadTime = [dict objectForKey:@"DataUploadTime"];
           
            
            NSString *weight = [dict objectForKey:@"Weight"];
            NSString *string2 = [DataUploadTime substringWithRange:NSMakeRange(5, 5)];
            NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
            [points addObject:weight];
            [dates addObject:string3];
            
        }
        scrollWidth = 100*[dataArray count];
        scrollView.contentSize = CGSizeMake(scrollWidth, scrollView.frame.size.height);
        
        PCLineChartView *chartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10, 10, scrollWidth, [self.view bounds].size.height-10)];
        chartView.interval = 20;
        chartView.maxValue = 120;
        chartView.minValue = 0;
        [scrollView addSubview:chartView];
        
        PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
        [component setTitle:@""];
        [component setPoints:points];
        [component setShouldLabelValues:YES];
        [component setColour:PCColorOrange];
        
        NSMutableArray *components = [[NSMutableArray alloc] init];
        [components addObject:component];
        [chartView setComponents:components];
        
        [chartView setXLabels:dates];
        
        
        
    }
    
    if(dataType==2)
    {
        
        
        
        NSMutableArray *points = [[NSMutableArray alloc] init];
        NSMutableArray *dates = [[NSMutableArray alloc] init];
        for (int i = 0; i <= dataArray.count -1; i++)
        {
            NSDictionary *dict = [dataArray objectAtIndex:i];
            
            NSString *DataUploadTime = [dict objectForKey:@"DataUploadTime"];
            
            
            NSString *weight = [dict objectForKey:@"UriaProtein"];
            NSString *string2 = [DataUploadTime substringWithRange:NSMakeRange(5, 5)];
            NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
            [points addObject:weight];
            [dates addObject:string3];
            
        }
        scrollWidth = 100*[dataArray count];
        scrollView.contentSize = CGSizeMake(scrollWidth, scrollView.frame.size.height);
        
        PCLineChartView *chartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10, 10, scrollWidth, [self.view bounds].size.height-10)];
        chartView.interval = 2;
        chartView.maxValue = 20;
        chartView.minValue = 0;
        [scrollView addSubview:chartView];
        
        PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
        [component setTitle:@""];
        [component setPoints:points];
        [component setShouldLabelValues:YES];
        [component setColour:PCColorOrange];
        
        NSMutableArray *components = [[NSMutableArray alloc] init];
        [components addObject:component];
        [chartView setComponents:components];
        
        [chartView setXLabels:dates];
        
        
        
    }
    if(dataType==3)
    {
        
        
        
        NSMutableArray *points = [[NSMutableArray alloc] init];
        NSMutableArray *dates = [[NSMutableArray alloc] init];
        for (int i = 0; i <= dataArray.count -1; i++)
        {
            NSDictionary *dict = [dataArray objectAtIndex:i];
            
            NSString *DataUploadTime = [dict objectForKey:@"DataUploadTime"];
            
            
            NSString *weight = [dict objectForKey:@"SystolicPressure"];
            NSString *string2 = [DataUploadTime substringWithRange:NSMakeRange(5, 5)];
            NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
            [points addObject:weight];
            [dates addObject:string3];
            
        }
        scrollWidth = 100*[dataArray count];
        scrollView.contentSize = CGSizeMake(scrollWidth, scrollView.frame.size.height);
        
        PCLineChartView *chartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10, 10, scrollWidth, [self.view bounds].size.height-10)];
        chartView.maxValue = 200;
        chartView.minValue = 50;
        chartView.interval = 20;
        [scrollView addSubview:chartView];
        
        PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
        [component setTitle:@""];
        [component setPoints:points];
        [component setShouldLabelValues:YES];
        [component setColour:PCColorOrange];
        
        NSMutableArray *components = [[NSMutableArray alloc] init];
        [components addObject:component];
        [chartView setComponents:components];
        
        [chartView setXLabels:dates];
        
        
        
    }
    
    if(dataType==4)
    {
        
        
        
        NSMutableArray *points = [[NSMutableArray alloc] init];
        NSMutableArray *dates = [[NSMutableArray alloc] init];
        for (int i = 0; i <= dataArray.count -1; i++)
        {
            NSDictionary *dict = [dataArray objectAtIndex:i];
            
            NSString *DataUploadTime = [dict objectForKey:@"DataUploadTime"];
            
            
            NSString *weight = [dict objectForKey:@"DiastolicPressure"];
            NSString *string2 = [DataUploadTime substringWithRange:NSMakeRange(5, 5)];
            NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
            [points addObject:weight];
            [dates addObject:string3];
            
        }
        scrollWidth = 100*[dataArray count];
        scrollView.contentSize = CGSizeMake(scrollWidth, scrollView.frame.size.height);
        
        PCLineChartView *chartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10, 10, scrollWidth, [self.view bounds].size.height-10)];
        chartView.maxValue = 200;
        chartView.minValue = 50;
        chartView.interval = 20;
        [scrollView addSubview:chartView];
        
        PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
        [component setTitle:@""];
        [component setPoints:points];
        [component setShouldLabelValues:YES];
        [component setColour:PCColorOrange];
        
        NSMutableArray *components = [[NSMutableArray alloc] init];
        [components addObject:component];
        [chartView setComponents:components];
        
        [chartView setXLabels:dates];
        
        
        
    }
    
    if(dataType==5)
    {
        
        
        
        NSMutableArray *points = [[NSMutableArray alloc] init];
        NSMutableArray *dates = [[NSMutableArray alloc] init];
        for (int i = 0; i <= dataArray.count -1; i++)
        {
            NSDictionary *dict = [dataArray objectAtIndex:i];
            
            NSString *DataUploadTime = [dict objectForKey:@"DataUploadTime"];
            
            
            NSString *weight = [dict objectForKey:@"BloodSugar"];
            NSString *string2 = [DataUploadTime substringWithRange:NSMakeRange(5, 5)];
            NSString *string3 = [string2 stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
            [points addObject:weight];
            [dates addObject:string3];
            
        }
        scrollWidth = 100*[dataArray count];
        scrollView.contentSize = CGSizeMake(scrollWidth, scrollView.frame.size.height);
        
        PCLineChartView *chartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10, 10, scrollWidth, [self.view bounds].size.height-10)];
        chartView.maxValue = 20;
        chartView.minValue = 0;
        chartView.interval = 2;
        [scrollView addSubview:chartView];
        
        PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
        [component setTitle:@""];
        [component setPoints:points];
        [component setShouldLabelValues:YES];
        [component setColour:PCColorOrange];
        
        NSMutableArray *components = [[NSMutableArray alloc] init];
        [components addObject:component];
        [chartView setComponents:components];
        
        [chartView setXLabels:dates];
        
        
        
    }


    
}

@end